package com.nuoniu.sibanyun.controller.finance;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.dto.AccountingLedgerDto;
import com.nuoniu.sibanyun.entity.finance.VAccountingLedger;
import com.nuoniu.sibanyun.entity.finance.VAccountingSearchProgramme;
import com.nuoniu.sibanyun.entity.vo.VAccountingLedgerVo;
import com.nuoniu.sibanyun.service.finance.IVAccountingLedgerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 科目总账 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2021-04-25
 */
@RestController
@RequestMapping("/finance/" +
        "accountingLedger")
@Api(tags = "科目总账管理")
public class VAccountingLedgerController extends BaseController {

    @Autowired
    private IVAccountingLedgerService accountingLedgerService;

    @ApiOperation("保存")
    @PostMapping("save")
    public HttpResult save(@RequestBody List<VAccountingLedgerVo> accountingLedger) {
        if(accountingLedger == null) {
            throw new NuoNiuException("科目不能为空");
        }

       return HttpResult.ok(accountingLedgerService.saveLedger(accountingLedger));
    }

    @ApiOperation("试算平衡")
    @GetMapping("trialBalance")
    public HttpResult trialBalance() {
        return HttpResult.ok(accountingLedgerService.trialBalance());
    }


    @ApiOperation("获取科目期初余额列表")
    @GetMapping("getList")
    public HttpResult<VAccountingLedgerVo> getList(Integer accountingTypeId, String name, String code) {
        return HttpResult.ok(accountingLedgerService.getList(accountingTypeId, name, code));
    }


    @ApiOperation("总分类账")
    @GetMapping("getGeneralLedgerList")
    public HttpResult<AccountingLedgerDto> getGeneralLedgerList(Page page, VAccountingSearchProgramme searchProgramme, Boolean isShowDetail, Boolean isDayBook) {
        if(StringUtils.isBlank(searchProgramme.getBeginDate())) {
            throw new NuoNiuException("请选择开始期间");
        }
        if(StringUtils.isBlank(searchProgramme.getEndDate())) {
            throw new NuoNiuException("请选择结束期间");
        }
//        if(StringUtils.isBlank(searchProgramme.getBeginCode())) {
//            throw new NuoNiuException("没有开始科目编码");
//        }
//        if(StringUtils.isBlank(searchProgramme.getEndCode())) {
//            throw new NuoNiuException("没有结束科目编码");
//        }

        return HttpResult.ok(accountingLedgerService.getGeneralLedgerList(page, searchProgramme, isShowDetail, isDayBook));
    }

//    @ApiOperation("科目明细账")
//    @GetMapping("getGeneralDetailLedgerList")
//    public HttpResult<AccountingLedgerDto> getGeneralDetailLedgerList(Page page, VAccountingSearchProgramme searchProgramme) {
//        if(StringUtils.isBlank(searchProgramme.getBeginDate())) {
//            throw new NuoNiuException("没有开始期间");
//        }
//        if(StringUtils.isBlank(searchProgramme.getEndDate())) {
//            throw new NuoNiuException("没有结束期间");
//        }
//        if(StringUtils.isBlank(searchProgramme.getBeginCode())) {
//            throw new NuoNiuException("没有开始科目编码");
//        }
//
//        return HttpResult.ok(accountingLedgerService.getGeneralDetailLedgerList(page, searchProgramme));
//    }

    @ApiOperation("发生额及科目余额表")
    @GetMapping("getPageList")
    public HttpResult<VAccountingLedger> pageEndAccountingList(Page page, VAccountingSearchProgramme searchProgramme) {
        if(StringUtils.isBlank(searchProgramme.getBeginDate())) {
            throw new NuoNiuException("没有开始期间");
        }
        if(StringUtils.isBlank(searchProgramme.getEndDate())) {
            throw new NuoNiuException("没有结束期间");
        }

        return HttpResult.ok( accountingLedgerService.pageEndAccountingList(page, searchProgramme));
    }
}
